package com.leyou.page.mq;

import com.leyou.constants.MQConstants;
import com.leyou.page.service.IPageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component  //注入spring容器
public class ItemListener {

    @Autowired
    private IPageService pageService;

    /**
     * 数据同步
     *接收item上架的消息,创建静态化页面
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            //设置队列
            value = @Queue(name = MQConstants.Queue.PAGE_ITEM_UP, durable = "true"),
            //设置交换机
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            //设置routingKey
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void listenInsert(Long id) {
        if (null != id) {
            pageService.createItemHtml(id);
        }
    }

    /**
     * 数据同步
     * 接收item下架的消息,删除静态化页面
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            //设置队列
            value = @Queue(name = MQConstants.Queue.PAGE_ITEM_DOWN, durable = "true"),
            //设置交换机
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            //设置routingKey
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void listenDelete(Long id) {
        if (null != id) {
            pageService.deleteItemHtml(id);
        }
    }
}
